namespace two {
    export class SelectScroller extends eui.Scroller {
        public constructor() {
            super();
        }

        leftArrow: egret.DisplayObject;
        rightArrow: egret.DisplayObject;

        private maxIndex: number = 0;

        private _selectIndex: number = 0;

        public get selectIndex(): number {
            return this._selectIndex;
        }

        public set selectIndex(v: number) {
            this._selectIndex = v;
            this.index = v;

            if (this.viewport) {
                (this.viewport as eui.List).selectedIndex = v;
                let w = this.viewport.width;
                this.viewport.scrollH = w * v;
            }

            this.setArrows();
        }

        private setArrows(): void {
            if (this.leftArrow && this.rightArrow) {
                if (this.maxIndex <= 0) {
                    this.leftArrow.visible = false;
                    this.rightArrow.visible = false;
                }
                else {
                    this.leftArrow.visible = true;
                    this.rightArrow.visible = true;
                    if (this._selectIndex == 0) {
                        this.leftArrow.visible = false;
                    }
                    else if (this._selectIndex == this.maxIndex) {
                        this.rightArrow.visible = false;
                    }
                }
            }
        }

        private setIdxArrows(): void {
            if (this.leftArrow && this.rightArrow) {
                if (this.maxIndex <= 0) {
                    this.leftArrow.visible = false;
                    this.rightArrow.visible = false;
                }
                else {
                    this.leftArrow.visible = true;
                    this.rightArrow.visible = true;
                    if (this.index <= 0) {
                        this.leftArrow.visible = false;
                    }
                    else if (this.index >= this.maxIndex) {
                        this.rightArrow.visible = false;
                    }
                }
            }
        }


        private isUp = false;
        private time = 0;
        private px = 0;
        private lastIdx = 0;
        protected createChildren(): void {
            super.createChildren();

            this.addEventListener(egret.Event.REMOVED_FROM_STAGE, (e: egret.Event) => {
                if (e.currentTarget == e.target) {
                    this.stage.removeEventListener(egret.TouchEvent.TOUCH_END, this.onEndHandler, this, true);
                }
            }, this);
            this.stage.addEventListener(egret.TouchEvent.TOUCH_END, this.onEndHandler, this, true);
            this.addEventListener(egret.TouchEvent.TOUCH_BEGIN, function (e: egret.TouchEvent): void {
                this.isUp = false;
                this.px = e.stageX;
                this.time = egret.getTimer();
            }, this, true);

            this.viewport.addEventListener(egret.Event.COMPLETE, ()=> {
                let cw = this.viewport.contentWidth;
                let w = this.viewport.width;
                this.maxIndex = Math.round(cw / w) - 1;
                this.selectIndex = this._selectIndex;
            }, this);
            one.callLater(() => {
                let cw = this.viewport.contentWidth;
                let w = this.viewport.width;
                this.maxIndex = Math.round(cw / w) - 1;
                this.selectIndex = this._selectIndex;
            }, 4);

            this.leftArrow.addEventListener(egret.TouchEvent.TOUCH_TAP, ()=> {
                this.index--;
                this.setIdxArrows();
                
                this.gotoEnd(this.index);
            }, this);
            this.rightArrow.addEventListener(egret.TouchEvent.TOUCH_TAP, ()=> {
                this.index++;
                this.setIdxArrows();
                
                this.gotoEnd(this.index);
            }, this);
        }

        private onEndHandler(e: egret.TouchEvent): void {
            let deltaTime = egret.getTimer() - this.time;

            let cw = this.viewport.contentWidth;
            let w = this.viewport.width;
            let sh = this.viewport.scrollH;

            let idx = Math.round(sh / w);
            let xx = e.stageX;
            if (xx < this.px && deltaTime < 200 && idx <= this.lastIdx) {//向右
                idx = this.lastIdx + 1;
            }
            else if (xx > this.px && deltaTime < 200 && idx >= this.lastIdx) {
                idx = this.lastIdx - 1;
            }

           this.gotoEnd(idx);
        }

        private gotoEnd(idx:number):void {
            this.isUp = true;

            let cw = this.viewport.contentWidth;
            let w = this.viewport.width;
            let sh = this.viewport.scrollH;

            let max = this.maxIndex = Math.round(cw / w) - 1;
            idx = Math.min(max, idx);
            idx = Math.max(0, idx);

            this.lastIdx = idx;
            this.index = idx;

            let rap = w * idx - this.viewport.scrollH;

            this.avg = rap / 20;
            this.count = 0;
            this.stopAnimation();
  
            egret.stopTick(this.onTicker, this);
            egret.startTick(this.onTicker, this);

            this.setIdxArrows();
        }

        private avg = 0;
        private count = 0;
        private index = 0;
        private onTicker(time: number): boolean {
            this.viewport.scrollH += this.avg;
            this.count++;
            if (this.count == 20) {
                let w = this.viewport.width;
                this.viewport.scrollH = w * this.index;
                egret.stopTick(this.onTicker, this);

                if (this.selectIndex != this.index) {
                    this.selectIndex = this.index;
                    (this.viewport as eui.List).selectedIndex = this.index;

                    this.dispatchEventWith(egret.Event.CHANGE);
                }
            }
            return false;
        }
    }
}